package com.mytest.common.jwt;


import com.mytest.common.constant.SecurityConstant;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

/**
 * @author cxb
 * @date 2019-07-25 17:08
 */
public class JwtUtil {

    private JwtUtil() {
    }

    ;


    /**
     * 根据用户名，过期时间创建jwt令牌
     *
     * @param userInfo   用户名
     * @param expireTime 过期时间 （分钟）
     * @return
     */
    public static String createJwt(String userInfo, Integer expireTime) {
        long now = System.currentTimeMillis();
        String token = Jwts.builder()
                //主题 放入用户名
                .setSubject(userInfo)
                //设置jwt的签发时间
                .setIssuedAt(new Date(now))
                //失效时间
                .setExpiration(new Date(now + expireTime * 60 * 1000))
                //签名算法和密钥
                .signWith(SignatureAlgorithm.HS512, SecurityConstant.JWT_SIGN_KEY)
                .compact();
        token = SecurityConstant.TOKEN_SPLIT + token;
        return token;
    }


    public static Claims parseJwt(String jwt) throws Exception {
        Claims claims = Jwts.parser()
                .setSigningKey(SecurityConstant.JWT_SIGN_KEY)
                .parseClaimsJws(jwt.replace(SecurityConstant.TOKEN_SPLIT, ""))
                .getBody();
        return claims;
    }


    public static void main(String[] args) throws Exception {
        String sysUserInfo = "{\"wxnickname\":\"打伞人\",\"headImgUrl\":\"http://thirdwx.qlogo.cn/mmopen/vi_32/fvMa3OE5eCuOibX9l3tXBADpb9ib6MiaZGw0Z4ZCLeYqRxwDKJ1ZgFSrPrWtDiaZsUoYlwoXaUDVsiaicZjb9wCGxWtw/132\",\"nickName\":\"刘宇\",\"sex\":1,\"grade\":\"黄金会员\",\"mobile\":\"18351867139\",\"id\":140,\"email\":\"\"}";

        String jwt = JwtUtil.createJwt(sysUserInfo, 1440);
        System.out.println("token :"+jwt);

        String token = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ7XCJ3eG5pY2tuYW1lXCI6XCLmiZPkvJ7kurpcIixcImhlYWRJbWdVcmxcIjpcImh0dHA6Ly90aGlyZHd4LnFsb2dvLmNuL21tb3Blbi92aV8zMi9mdk1hM09FNWVDdU9pYlg5bDN0WEJBRHBiOWliNk1pYVpHdzBaNFpDTGVZcVJ4d0RLSjFaZ0ZTclByV3REaWFac1VvWWx3b1hhVURWc2lhaWNaamI5d0NHeFd0dy8xMzJcIixcIm5pY2tOYW1lXCI6XCLliJjlrodcIixcInNleFwiOjEsXCJncmFkZVwiOlwi6buE6YeR5Lya5ZGYXCIsXCJtb2JpbGVcIjpcIjE4MzUxODY3MTM5XCIsXCJpZFwiOjE0MCxcImVtYWlsXCI6XCJcIn0iLCJpYXQiOjE1NzgwMjgzNzgsImV4cCI6MTU3ODExNDc3OH0.\n" +
                "yshlLpq1xf7sJx11uK9_vWvRP-N1FMlCN2LlXqKRyNQUtpTcK5rv1y55T6Gj_ZGd51Vs91cmu4GdvoN9mUcwdA";

        String token1 = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ7XCJ3eG5pY2tuYW1lXCI6XCLmiZPkvJ7kurpcIixcImhlYWRJbWdVcmxcIjpcImh0dHA6Ly90aGlyZHd4LnFsb2dvLmNuL21tb3Blbi92aV8zMi9mdk1hM09FNWVDdU9pYlg5bDN0WEJBRHBiOWliNk1pYVpHdzBaNFpDTGVZcVJ4d0RLSjFaZ0ZTclByV3REaWFac1VvWWx3b1hhVURWc2lhaWNaamI5d0NHeFd0dy8xMzJcIixcIm5pY2tOYW1lXCI6XCLliJjlrodcIixcInNleFwiOjEsXCJncmFkZVwiOlwi6buE6YeR5Lya5ZGYXCIsXCJtb2JpbGVcIjpcIjE4MzUxODY3MTM5XCIsXCJpZFwiOjE0MCxcImVtYWlsXCI6XCJcIn0iLCJpYXQiOjE1NzgwMzA3NjQsImV4cCI6MTU3ODExNzE2NH0.3ygZNBN5zpmnxgC78S-rcCj71o-vBQwPMFOBMvnzsuEO_zxkKJCDE9lc3CmCt7liu_DulGZEZsDdC_ImoB-uLQ";

        String token2 = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJz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cuJCXD9oWU5_FVMzbUiWSpQQsp1sdKz7q0nKAinqaL9A";

        String token4 = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ7XCJ3eG5pY2tuYW1lXCI6XCLmiZPkvJ7kurpcIixcImhlYWRJbWdVcmxcIjpcImh0dHA6Ly90aGlyZHd4LnFsb2dvLmNuL21tb3Blbi92aV8zMi9mdk1hM09FNWVDdU9pYlg5bDN0WEJBRHBiOWliNk1pYVpHdzBaNFpDTGVZcVJ4d0RLSjFaZ0ZTclByV3REaWFac1VvWWx3b1hhVURWc2lhaWNaamI5d0NHeFd0dy8xMzJcIixcIm5pY2tOYW1lXCI6XCLliJjlrodcIixcInNleFwiOjEsXCJncmFkZVwiOlwi6buE6YeR5Lya5ZGYXCIsXCJtb2JpbGVcIjpcIjE4MzUxODY3MTM5XCIsXCJpZFwiOjE0MCxcImVtYWlsXCI6XCJcIn0iLCJpYXQiOjE1NzgwMzEzOTAsImV4cCI6MTU3ODExNzc5MH0.ClesS25DYoNOIIxEqX9z8symWwx_wYjzXXLw43PuDJcuJCXD9oWU5_FVMzbUiWSpQQsp1sdKz7q0nKAinqaL9A";
        //String token = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImlhdCI6MTU0Mjg2OTQwMiwiZXhwIjoxNTQyODcxMjAyfQ.7NWa6dEx9DQxM0LkwpJ3o45FWW7zVJaUTwrl0NXgXRD_fdE2-wkqW8tUNnVm-VDyk5aB_BfMUI6y-ezZ_Kr_hg";
//        HospitalDoctor doctor = new HospitalDoctor().setId(Long.parseLong("1")).setDoctorName("盖伦111").setWorkNum("12435").setIsLogin(1);


//        String jwt = createJwt(JSON.toJSONString(doctor), 30);
//        System.out.println(jwt);

        try {
            Claims claims = parseJwt(token4);
            System.out.println("1111"+claims);
        } catch (Exception e) {
            System.out.println("Exception:"+e);
            e.printStackTrace();
        }

    }

}